package com.csjy.migration.hnnd.business;

import com.csjy.migration.hnnd.BusinessTool;
import com.csjy.migration.hnnd.DbUtil;
import com.csjy.migration.hnnd.ProgramArgument;
import com.csjy.migration.hnnd.annotation.BusinessDataConverter;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@BusinessDataConverter(
    auto = false,
    insertSql = "INSERT INTO T_P_BPM_LOG\n" +
        "(ID, BPM_ID, OP_NAME, OP_TIME, OP_CODE, OP_TYPE, TASK_NAME, TASK_NODE, MEMO, FILE_ID, TASK_ID)" +
        "VALUES(?,?,?,?,?,?,?,?,?,?,?)",
    updateSql = "UPDATE T_P_BPM_LOG\n" +
        "SET BPM_ID=?, OP_NAME=?, OP_TIME=?, OP_CODE=?, OP_TYPE=?, TASK_NAME=?, TASK_NODE=?, MEMO=?, FILE_ID=?, TASK_ID=?\n" +
        "WHERE ID = ?",
    findMigratedIdListSql = "SELECT ID FROM T_P_BPM_LOG WHERE BPM_ID = ?"
)
public class TPBpmLog extends AbstractWorkflowBusiness {
  public TPBpmLog(DbUtil sourceDbUtil, DbUtil targetDbUtil, SourceBusinessData sourceData, ProgramArgument programArgument, String projectId, String procInstId) throws SQLException {
    super(sourceDbUtil, targetDbUtil, sourceData, programArgument, projectId, procInstId);
  }

  @Override
  public void migrate() throws Exception {

  }

  public void customMigrate(Map taskRow,Map procInstRow, Object opCode,String memoStr) throws SQLException {
    String targetId = this.procInstId +"-" + taskRow.get("ID_").toString();
    int needMigratedResult = this.needMigrated(targetId);
    if(needMigratedResult == 0) {
      return;
    }
    if(needMigratedResult == 1) {
      this.targetDbUtil.updateByParams(updateSql,this.buildValues(taskRow,procInstRow,opCode,memoStr),true);
//      this.batchUpdateValues.add(this.buildValues(taskRow,opCode,memoStr));
    } else {
      this.targetDbUtil.updateByParams(insertSql,this.buildValues(taskRow,procInstRow,opCode,memoStr));
//      this.batchInsertValues.add(this.buildValues(taskRow,opCode,memoStr));
    }
    this.buildValues(taskRow,procInstRow, opCode,memoStr);
  }

  public List buildValues(Map taskRow, Map procInstRow, Object opCode,String memoStr) {
    String targetId = this.procInstId+"-" + taskRow.get("ID_").toString();
    String assigneeEmpCode = null;
    Object actorId = taskRow.get("ACTORID_");

    if(!(actorId == null || actorId.equals(""))) {
      assigneeEmpCode =  BusinessTool.getTaskRealActorId(taskRow.get("ACTORID_").toString(),taskRow.get("CREATE_"));
    }

    String[] taskDefNameAndKey = BusinessTool.getTaskDefNameAndKey(procInstRow,
        taskRow.get("PROCDEF_NAME_").toString(),
        taskRow.get("TASK_DEF_NAME_").toString());

    String opType = taskDefNameAndKey[1]+":" +taskRow.get("ID_")+"-JW"+":"+assigneeEmpCode;

    String taskNodeQualifiedName = taskRow.get("NAME_").toString();
    String[] taskNodeQualifiedNameArr = taskNodeQualifiedName.split("@");

    List values = new ArrayList();
    values.add(targetId);
    // BPM_ID
    values.add(this.procInstId+"-JW");
    // OP_NAME
    values.add(taskRow.get("EMPNAME"));
    // OP_TIME
    values.add(getTaskOpTime(taskRow));
    // OP_CODE this.curProcessData.get("modelName")
    values.add(opCode);
    // OP_TYPE
    values.add(opType);
    // TASK_NAME
    values.add(taskNodeQualifiedNameArr[0]);
    // TASK_NODE
    values.add(taskDefNameAndKey[0]);
    // MEMO
    values.add(memoStr);
    // TODO:FILE_ID
    values.add(null);
    // TASK_ID
    values.add(taskRow.get("ID_")+"-JW");

    return values;
  }

  private Object getTaskOpTime(Map taskRow) {

    Object end = taskRow.get("END_");
    if(!(end == null || end.toString().equals(""))) {
      return end;
    }
    Object start = taskRow.get("START_");
    if(!(start == null || start.toString().equals(""))) {
      return start;
    }

    return taskRow.get("CREATE_");


  }
}
